package com.betty.cscams.service.impl;

import java.util.Date;
import java.util.List;

import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.dao.BusTermMapper;
import com.betty.cscams.dao.SysDeptMapper;
import com.betty.cscams.dao.SysUserMapper;
import com.betty.cscams.entity.*;
import com.betty.cscams.util.EasyPoiUtil;
import com.betty.cscams.util.SecurityUtils;
import com.betty.cscams.vo.ScoreQueryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.betty.cscams.common.page.MybatisPageHelper;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.common.page.PageResult;

import com.betty.cscams.dao.BusMoralMapper;
import com.betty.cscams.service.BusMoralService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;

/**
 * ---------------------------
 *  (BusMoralServiceImpl)         
 * ---------------------------
 * 时间：  2022-03-14 16:41:44
 * ---------------------------
 */
@Service
public class BusMoralServiceImpl implements BusMoralService {

	@Autowired
	private BusMoralMapper busMoralMapper;
	@Autowired
	private BusTermMapper busTermMapper;
	@Autowired
	private SysDeptMapper sysDeptMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public int save(BusMoral record) {
		record.setLastUpdateBy(SecurityUtils.getUsername());
		record.setLastUpdateTime(new Date());
		if(record.getId() == null || record.getId() == 0) {
			record.setCreateBy(SecurityUtils.getUsername());
			record.setCreateTime(new Date());
			record.setStatus(SysConstants.REVIEW_STATUS_1);
			return busMoralMapper.add(record);
		}
		return busMoralMapper.update(record);
	}

	@Override
	public int delete(BusMoral record) {
		return busMoralMapper.delete(record.getId());
	}

	@Override
	public int delete(List<BusMoral> records) {
		for(BusMoral record:records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public BusMoral findById(Long id) {
		return busMoralMapper.findById(id);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest) {
		Object name = pageRequest.getParam("name");
		Object  deptId = pageRequest.getParam("deptId").toString();
		Object  termId = pageRequest.getParam("termId").toString();
		Object  status = pageRequest.getParam("status").toString();
		String student = "";
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		if("student".equals(sysUser.getType())){
			student = sysUser.getName();
		}
		return MybatisPageHelper.findPage(pageRequest, busMoralMapper,"findPage",name,deptId,termId,student,status);
	}

	/**
	 * 根据学号和学期删除数据
	 * @param record
	 */
	@Override
	public void deleteByEntity(BusMoral record) {
		busMoralMapper.deleteByEntity(record);
	}

	/**
	 * 按班级计算成绩
	 * @param termId
	 * @param deptId
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void calculationSports(Long termId, Long deptId) {
		busMoralMapper.calculationTotal(termId,deptId);
		List<BusMoral> list = busMoralMapper.selectOrder(termId,deptId);
		for (BusMoral record:list) {
			busMoralMapper.calculationOrder(record);
		}
	}

	/**
	 * 清空计算
	 * @param termId
	 * @param deptId
	 */
	@Override
	public void calculationReset(Long termId, Long deptId) {
		busMoralMapper.calculationReset(termId,deptId);
	}

	/**
	 * 到处班级 成绩
	 * @param params
	 */
	@Override
	public void exportMoral(ScoreQueryBean params, HttpServletResponse response) {
		BusTerm busTerm =busTermMapper.findById(params.getTermId());
		SysDept sysDept = sysDeptMapper.selectByPrimaryKey(params.getDeptId());
		List<BusMoral> list = busMoralMapper.findAllByParams(params);
		String title = sysDept.getName()+"德育成绩表";
		String sheetName = "德育";
		String fileName = sysDept.getName()+"德育成绩表.xlsx";
		EasyPoiUtil.exportExcel(list,title,sheetName,BusMoral.class,fileName,true,response);
	}

	@Override
	public PageResult findPageTo(PageRequest pageRequest) {
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		Object name = pageRequest.getParam("name");
		Object  deptId = pageRequest.getParam("deptId").toString();
		Object  termId = pageRequest.getParam("termId").toString();
		Object  status = pageRequest.getParam("status").toString();
		String student = "";
		if("student".equals(sysUser.getType())){
			student = sysUser.getName();
		}
		return MybatisPageHelper.findPage(pageRequest, busMoralMapper,"findPageTo",name,deptId,termId,sysUser.getId(),student,status);
	}
}
